package top.wjgyy.tools.ddns.node;


import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.stage.Stage;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.util.Arrays;

public class SystemTrayNode {


    private static SystemTrayNode instance;

    private MenuItem openMenu;
    private MenuItem closeMenu;
    private PopupMenu popupMenu;
    private TrayIcon trayIcon;
    private SystemTray tray;
    private Stage stage;
    private boolean isInit = false;

    public boolean isInit() {
        return isInit;
    }

    public static SystemTrayNode getInstance() {
        instance = instance == null ? new SystemTrayNode() : instance;
        return instance;
    }

    public SystemTrayNode() {
        openMenu = new MenuItem("打开");
        closeMenu = new MenuItem("关闭");
        popupMenu = new PopupMenu();
        popupMenu.add(openMenu);
        popupMenu.add(closeMenu);
        trayIcon = new TrayIcon(new Image() {
            @Override
            public int getWidth(ImageObserver observer) {
                return 0;
            }

            @Override
            public int getHeight(ImageObserver observer) {
                return 0;
            }

            @Override
            public ImageProducer getSource() {
                return null;
            }

            @Override
            public Graphics getGraphics() {
                return null;
            }

            @Override
            public Object getProperty(String name, ImageObserver observer) {
                return null;
            }
        });
        trayIcon.setImageAutoSize(true);
        trayIcon.setPopupMenu(popupMenu);
        //添加鼠标点击事件
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent e) {
                //双击左键
                if (e.getButton() == java.awt.event.MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                    showStage();
                }
            }
        });
        //获取系统托盘
        tray = SystemTray.getSystemTray();
    }

    public void listen(Stage stage){
        this.stage = stage;
        Platform.setImplicitExit(false);
        if (stage.getIcons().size() == 0) throw new RuntimeException("未检测到图标！");
        trayIcon.setImage(SwingFXUtils.fromFXImage(stage.getIcons().get(0), null));
        trayIcon.setToolTip(stage.getTitle() + "后台运行");
        openMenu.addActionListener(e -> {
            showStage();
        });
        //菜单项(退出)
        closeMenu.addActionListener(e -> {
            System.exit(0);
        });
        isInit = true;
    }

    /*public void init(Stage stage){
        this.stage = stage;
        Platform.setImplicitExit(false);
        if (stage.getIcons().size() == 0) throw new RuntimeException("未检测到图标！");
        trayIcon.setImage(SwingFXUtils.fromFXImage(stage.getIcons().get(0), null));
        trayIcon.setToolTip(stage.getTitle() + "后台运行");
        // trayIcon = new TrayIcon(SwingFXUtils.fromFXImage(stage.getIcons().get(0), null), stage.getTitle() + "后台运行");
        // trayIcon.setImageAutoSize(true);
        openMenu.addActionListener(e -> {
            showStage();
        });
        //菜单项(退出)
        closeMenu.addActionListener(e -> {
            System.exit(0);
        });
        isInit = true;
    }*/

    public void show() throws AWTException {
        if (Arrays.asList(tray.getTrayIcons()).contains(trayIcon)) return;
        tray.add(trayIcon);
    }

    public void hide(){
        Platform.runLater(() -> {
            //如果支持系统托盘,就隐藏到托盘,不支持就直接退出
            if (SystemTray.isSupported()) {
                //stage.hide()与stage.close()等价
                stage.hide();
            } else {
                System.exit(0);
            }
        });
    }

    /**
     * 点击系统托盘,显示界面(并且显示在最前面,将最小化的状态设为false)
     */
    private void showStage(){
        //点击系统托盘,
        Platform.runLater(() -> {
            if(stage.isIconified()){ stage.setIconified(false);}
            if(!stage.isShowing()){ stage.show(); }
            stage.toFront();
        });
        tray.remove(trayIcon);
    }

    public void close(){
        stage.hide();
        System.exit(0);
    }

    public void remove(){
        Platform.setImplicitExit(true);
        if (Arrays.asList(tray.getTrayIcons()).contains(trayIcon)) tray.remove(trayIcon);
    }
}
